package com.pixiv.tools;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;

public class FileUtilTool {
    
    public static void deleteTempNoName(String path,String name)
    {
        File f = new File(path);
        //System.out.println(f.getAbsolutePath() );
        if (f.isDirectory())
        {
            for (File sub_f:f.listFiles()){
                if (name.equals(sub_f.getName()))
                {
                    //icon not delete;
                }else{
                    sub_f.delete();
                }
            }
        }

    }

    public static void DeleteFile(String path)
    {
        File f = new File(ProgramStaticGlobal.getPRealPath() + path);
        if (f.exists())
            f.delete();
    }

    public static void CopyFile(String filePath1,String filePath2)
    {
        try {
            String path = ProgramStaticGlobal.getPRealPath();

            String Video1_Path = path + filePath1;
            String Video2_Path = path + filePath2;

            FileInputStream fis = new FileInputStream(Video1_Path);
            FileOutputStream fos = new FileOutputStream(Video2_Path);
            byte[] b = new byte[1024];
            int len = fis.read(b,0,b.length);
            //long startT = new Date().getTime();
            while (len != -1)
            {
                fos.write(b);
                len = fis.read(b,0,b.length);
                //System.out.println(len);
            }
            //System.out.println("写出成功，用时:" + (new Date().getTime() - startT) + "毫秒");
            //差距太明显了.
            fis.close();
            fos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //缩小图片
    public static OutputStream resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
        BufferedImage prevImage = ImageIO.read(is);
        double width = prevImage.getWidth();
        double height = prevImage.getHeight();
        double percent = size/width;
        int newWidth = (int)(width * percent);
        int newHeight = (int)(height * percent);
        BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
        Graphics graphics = image.createGraphics();
        graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
        ImageIO.write(image, format, os);
        os.flush();
        is.close();
        os.close();
        return os;
    }


    //转换列表对象变成 json对象
    public static String ConvertListToPageJson(List<?> list, int countList) {
        // 新建一个json数组
        JSONArray jsonArray = new JSONArray();
        // 新建一个json对象
        JSONObject jsonObject;
        // 遍历泛型集合
        for (Object object : list) {
            jsonObject = new JSONObject(object);
            jsonArray.put(jsonObject);
        }
        // 转换数据格式
        String json = jsonArray.toString();
        // 拼接字符串
        StringBuffer sb = new StringBuffer();
        sb.append("{\"count\":");
        sb.append(countList);
        sb.append(",\"code\":");
        sb.append(0);
        sb.append(",\"msg\":");
        sb.append("null");
        sb.append(",\"data\":");
        sb.append(json);
        sb.append("}");
        String jsonString = sb.toString();
        return jsonString;
    }

}

